//
//  SEGOperatingGuideView.m
//  SEGUhomeSommunitySocial
//
//  Created by 揭婧雯 on 2019/4/22.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGOperatingGuideView.h"

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

@interface SEGOperatingGuideView ()
/**
 图片
 */
@property (nonatomic, strong)UIImageView *imageView;
/**
 图片数租
 */
@property (nonatomic, strong)NSArray *imageArray;
/**
 图片坐标数租
 */
@property (nonatomic, strong)NSArray *frameValueArray;

@end

@implementation SEGOperatingGuideView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.frame = CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight());
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seg_hiddenGuideView:)];
    [self addGestureRecognizer:tap];
}

+ (void)showGuidViewWithUserDefaultKey:(NSString *)key imageArray:(NSArray *)imageArray frameValueArray:(NSArray *)frameValueArray
{
    if (!imageArray || imageArray.count == 0) {
        return;
    }
    if (!frameValueArray || frameValueArray.count == 0) {
        return;
    }
    
    NSString *hasShowed = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if(!hasShowed) {//显示引导蒙层
    
        SEGOperatingGuideView *guideView = [[SEGOperatingGuideView alloc] init];
        [[UIApplication sharedApplication].keyWindow addSubview:guideView];
        guideView.imageArray = imageArray;
        guideView.frameValueArray = frameValueArray;
        
        
        CGRect frame = [frameValueArray[0] CGRectValue];
        guideView.imageView = [[UIImageView alloc] initWithFrame:frame];
        guideView.imageView.image = imageArray[0];
        guideView.imageView.userInteractionEnabled = YES;
        guideView.imageView.tag = 1; //记录当前为第几张图片
        [guideView addSubview:guideView.imageView];
        
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

// 隐藏引导蒙层
- (void)seg_hiddenGuideView:(UITapGestureRecognizer *)tap
{
    if (self.imageView.tag < self.imageArray.count && self.imageView.tag < self.frameValueArray.count) {
        self.imageView.tag++;
        NSInteger index = self.imageView.tag - 1;
        CGRect frame = [self.frameValueArray[index] CGRectValue];
        self.imageView.frame = frame;
        self.imageView.image = self.imageArray[index];
    }else {
        [tap.view removeFromSuperview];
    }
}

@end
